package com.s.core.root.strategy.factor;

import com.s.core.root.D;

import java.util.HashMap;
import java.util.Map;

public class ZtActivity {
    public static final int ZT_ACT_TOTAL = 100;
    public static final int ZT_ACT_FACTOR_ZT_SCORE = 24;
    public static final Map<Integer, Integer> ZT_ACT_FACTOR_INTER_SCORE;
    public static final int ZT_ACT_FACTOR_HISTORY = 25;
    public static final int ZT_ACT_FACTOR_RECENT = 5;
    public static final int ZT_ACT_FACTOR_SY = 4;
    public static final Map<Integer, Integer> ZT_ACT_FACTOR_RECENT_SCORE;
    public static final Map<Integer, Integer> ZT_ACT_FACTOR_HISTORY_SCORE;
    public static final Map<Integer, Integer> ZT_ACT_FACTOR_SY_SCORE;
    public static final int ZT_ACT_FACTOR_20CM_SCORE = 12;

    static {
        ZT_ACT_FACTOR_INTER_SCORE = new HashMap<>();
        ZT_ACT_FACTOR_INTER_SCORE.put(0, 24);
        ZT_ACT_FACTOR_INTER_SCORE.put(1, 12);
        ZT_ACT_FACTOR_INTER_SCORE.put(2, 12);
        ZT_ACT_FACTOR_INTER_SCORE.put(3, 8);
        ZT_ACT_FACTOR_INTER_SCORE.put(4, 8);
        ZT_ACT_FACTOR_INTER_SCORE.put(5, 8);
    }

    static {
        ZT_ACT_FACTOR_SY_SCORE = new HashMap<>();
        ZT_ACT_FACTOR_SY_SCORE.put(9, 8);
        ZT_ACT_FACTOR_SY_SCORE.put(8, 4);
    }

    static {
        ZT_ACT_FACTOR_RECENT_SCORE = new HashMap<>();
        ZT_ACT_FACTOR_RECENT_SCORE.put(0, 16);
        ZT_ACT_FACTOR_RECENT_SCORE.put(1, 12);
        ZT_ACT_FACTOR_RECENT_SCORE.put(2, 8);
        ZT_ACT_FACTOR_RECENT_SCORE.put(3, 4);
    }

    static {
        ZT_ACT_FACTOR_HISTORY_SCORE = new HashMap<>();
        ZT_ACT_FACTOR_HISTORY_SCORE.put(0, 0);
        ZT_ACT_FACTOR_HISTORY_SCORE.put(1, -8);
        ZT_ACT_FACTOR_HISTORY_SCORE.put(2, -12);
        ZT_ACT_FACTOR_HISTORY_SCORE.put(3, -16);
    }

    /**
     * 计算涨停活跃度
     */
    public static int calculate(D d) {
        int[] z = ztInt(d);
        int totalScore = 0;
        for (int i = 0; i < z.length; i++) {
            if (z[i] > 0) {
                totalScore += calculate(d, i, z);
            }
        }
        return totalScore;
    }

    private static int[] ztInt(D d) {
        int[] z = new int[ZT_ACT_TOTAL];
        for (int offset = 0; offset < z.length; offset++) {
            D bd = d.bDaily(offset);
            if (bd == null) {
                z[offset] = 0;
                continue;
            }
            if (bd.isZt()) {
                z[offset] = 10;
                continue;
            }
            Integer syValue = bd.syValue();
            if (syValue != null) {
                z[offset] = syValue;
                continue;
            }
            z[offset] = 0;
        }
        return z;
    }

    private static int calculate(D d, int i, int[] z) {
        if (z[i] == 10) {
            return calculate10(d, i, z);
        }
        if (i <= ZT_ACT_FACTOR_SY) {
            Integer syScore = ZT_ACT_FACTOR_SY_SCORE.get(z[i]);
            if (syScore != null) {
                return syScore;
            }
        }
        return 0;
    }

    private static int calculate10(D d, int i, int[] z) {
        int totalScore = ZT_ACT_FACTOR_ZT_SCORE;
        //inter zt score
        for (int j = i + 1; j < Math.min(i + 1 + ZT_ACT_FACTOR_INTER_SCORE.size(), z.length); j++) {
            if (z[j] == 10) {
                int interval = j - i - 1;
                totalScore += ZT_ACT_FACTOR_INTER_SCORE.get(interval);
                break;
            }
        }
        //recent zt score
        Integer recentScore = ZT_ACT_FACTOR_RECENT_SCORE.get(i / ZT_ACT_FACTOR_RECENT);
        if (recentScore != null) {
            totalScore += recentScore;
        }
        //history zt score
        totalScore += ZT_ACT_FACTOR_HISTORY_SCORE.get(i / ZT_ACT_FACTOR_HISTORY);
        //20cm zt score
        if (d.isNot10cm()) {
            totalScore += ZT_ACT_FACTOR_20CM_SCORE;
        }
        return totalScore;
    }
}
